﻿Public Class frmDanhMuc
    Private ctlDanhMuc As New CDanhMucController
    Private currDanhMuc As tblDanhMuc
    Private sSearch As String = ""

    Private Sub ucCountries_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'hiển thị danh sách các nước
        LoadListView()
        'hien thi nut them, xoa, sua, thoat
        HienThiNut_Thuong()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

    'Private Sub frmDanhMuc_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
    '    If (e.KeyCode = Keys.N AndAlso e.Modifiers = Keys.Control) Then
    '        'thêm mới
    '        btnAdd_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.E AndAlso e.Modifiers = Keys.Control) Then
    '        'Sửa
    '        btnEdit_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.Delete) Then
    '        'xoá
    '        btnDel_Click(Nothing, Nothing)
    '    End If
    'End Sub
#Region "Button Event"
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If (MessageBox.Show("Bạn có chắc muốn xoá " + currDanhMuc.Ma + " ?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
            ctlDanhMuc.DeleteRecord(currDanhMuc.ID_DanhMuc)
            'load lại grid
            LoadListView()
        End If
    End Sub
    Private Function LuuThongTin() As Boolean
        If (txtCode.Text = "") Then
            Return False
        End If
        Dim ma As String = txtCode.Text
        Dim trungTen As Boolean = False
        'kiểm tra xem tên có bị trùng hay không
        If (ctlDanhMuc.CheckIfExists(currDanhMuc.ID_DanhMuc, ma)) Then
            MessageBox.Show("Đã tồn tại danh mục này.")
            Return False
        End If

        'luu du lieu
        'tách ra thành từng cụm
        Dim temp As String() = ma.Split(New Char() {"-"}, StringSplitOptions.RemoveEmptyEntries)
        With currDanhMuc
            .Muc = temp(0)
            .TieuMuc = temp(1)
            .TietMuc = temp(2)
            .Nganh = temp(3)
            .Ma = ma
            .NoiDung = txtNoiDung.Text
        End With
        Dim idCurr As Integer = currDanhMuc.ID_DanhMuc
        If currDanhMuc.ID_DanhMuc > 0 Then
            ctlDanhMuc.UpdateRecordWithoutAttach(currDanhMuc)
        Else
            ctlDanhMuc.InsertRecord(currDanhMuc)
            idCurr = currDanhMuc.ID_DanhMuc
        End If
        LoadListView()
        'Dim indexRow As Integer = -1
        ''lấy ra vị trí của port vừa thêm hoặc chỉnh sửa vào
        'indexRow = FindIndexOfPortInGrid(idCurr)
        ''Set current row
        ''gridDanhMuc.CurrentCell = gridDanhMuc.Rows(indexRow).Cells(1)
        ''gridDanhMuc.Rows(indexRow).Selected = True
        ''tro ve trạng thái thường
        HienThiNut_Thuong()
        Return True
    End Function
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        LuuThongTin()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        HienThiNut_Sua()
        RefreshForm()
        'Init obj
        currDanhMuc = New tblDanhMuc
        txtCode.Focus()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'gridDanhMuc.ReadOnly = False
        'gridDanhMuc.AllowUserToAddRows = False
        HienThiNut_Sua()
        txtCode.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Dim _id As Integer = gridDanhMuc.CurrentRow.Cells("ID_DanhMuc").Value
        'currDanhMuc = ctlDanhMuc.SelectRecordByID(_id)
        FillDanhMucForm()
        'hien thi lai trang thai binh thuong
        HienThiNut_Thuong()
    End Sub
#End Region

#Region "Utilities"
    'Private Function FindIndexOfPortInGrid(ByVal id As Integer) As Integer
    '    Dim index As Integer = 0
    '    For Each row As DataGridViewRow In gridDanhMuc.Rows
    '        If (row.Cells("ID_DanhMuc").Value = id) Then
    '            index = row.Index
    '            Exit For
    '        End If
    '    Next
    '    Return index
    'End Function
    Private Sub FillDanhMucForm()
        If (currDanhMuc IsNot Nothing) Then
            txtCode.Text = currDanhMuc.Ma
            txtNoiDung.Text = currDanhMuc.NoiDung
        Else
            txtCode.Text = ""
            txtNoiDung.Text = ""
        End If
    End Sub
    Public Sub RefreshForm()
        'txtMuc.Text = ""
        'txtTieuMuc.Text = ""
        'txtTietMuc.Text = ""
        txtCode.Text = ""
        txtNoiDung.Text = ""
    End Sub
    Private Sub EnableControls(ByVal val As Boolean)
        txtCode.Enabled = val
        txtNoiDung.Enabled = val
        lvDanhMuc.Enabled = Not val
    End Sub
    Private Sub HienThiNut_Thuong()
        'gridDanhMuc.AllowUserToAddRows = False
        'gridDanhMuc.AllowUserToDeleteRows = False
        'gridDanhMuc.ReadOnly = True
        EnableControls(False)
        btnAdd.Enabled = True
        btnDel.Enabled = True
        btnEdit.Enabled = True
        btnUpdate.Enabled = False
        btTiep.Enabled = False
        btnCancel.Enabled = False
    End Sub
    Private Sub HienThiNut_Sua()
        EnableControls(True)
        btnAdd.Enabled = False
        btnDel.Enabled = False
        btnEdit.Enabled = False
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
        btTiep.Enabled = True
    End Sub
#End Region

#Region "ListView"
    Public Sub LoadListView()
        lvDanhMuc.BeginUpdate()
        lvDanhMuc.Items.Clear()
        'lay ra tat ca cac danh muc con thuoc ma cha do
        Dim list As IQueryable(Of tblDanhMuc) = ctlDanhMuc.SelectAllRecord()
        For Each d As tblDanhMuc In list
            Dim item As New ListViewItem(d.Muc)
            item.SubItems.Add(d.TieuMuc)
            item.SubItems.Add(d.TietMuc)
            item.SubItems.Add(d.Nganh)
            item.SubItems.Add(d.NoiDung)
            item.Name = d.ID_DanhMuc
            lvDanhMuc.Items.Add(item)
        Next
        lvDanhMuc.EndUpdate()
        If (lvDanhMuc.Items.Count > 0) Then
            lvDanhMuc.Items(0).Selected = True
        End If
    End Sub
    Private Sub lvChungTu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvDanhMuc.SelectedIndexChanged
        If (lvDanhMuc.SelectedItems.Count > 0) Then
            Dim idDanhMuc As Integer = Integer.Parse(lvDanhMuc.SelectedItems(0).Name.ToString)
            'lấy thông tin danh muc
            currDanhMuc = ctlDanhMuc.SelectRecordByID(idDanhMuc)
            FillDanhMucForm()
        Else
            currDanhMuc = Nothing
        End If
    End Sub
#End Region

    Private Sub btTiep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTiep.Click
        If (LuuThongTin()) Then
            btnAdd_Click(Nothing, Nothing)
        End If
    End Sub
End Class